﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;

namespace Assist
{
	public class FileUtil
	{
		public static byte[] ReadFile(string fileName)
		{
			FileStream fileStream = null;
			byte[] array = new byte[0];
			byte[] result;
			try
			{
				fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
				BinaryReader binaryReader = new BinaryReader(fileStream);
				binaryReader.BaseStream.Seek(0L, SeekOrigin.Begin);
				array = binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
				result = array;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
				result = array;
			}
			finally
			{
				if (fileStream != null)
				{
					fileStream.Close();
				}
			}
			return result;
		}

		public static bool WriteFile(byte[] pReadByte, string fileName)
		{
			FileStream fileStream = null;
			try
			{
				fileStream = new FileStream(fileName, FileMode.OpenOrCreate);
				fileStream.Write(pReadByte, 0, pReadByte.Length);
			}
			catch
			{
				return false;
			}
			finally
			{
				if (fileStream != null)
				{
					fileStream.Close();
				}
			}
			return true;
		}

		public static FileInfo GetProjConfigFile(Autodesk.Revit.DB.Document doc)
		{
			string tempPath = Path.GetTempPath();
			string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(doc.Title);
			string fileName = tempPath + "\\" + fileNameWithoutExtension + "_hy.xml";
			FileInfo fileInfo = new FileInfo(fileName);
			if (!fileInfo.Exists)
			{
				FileUtil.CreateProjXmlFile(fileInfo.FullName);
			}
			return fileInfo;
		}

		public static FileInfo GetTempDataFile()
		{
			FileInfo fileInfo = new FileInfo(Path.GetTempPath() + "\\Temp.xml");
			if (!fileInfo.Exists)
			{
				FileUtil.CreateTempXmlFile(fileInfo.FullName);
			}
			return fileInfo;
		}

		public static void CreateProjXmlFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
			xmlDocument.AppendChild(newChild);
			XmlElement newChild2 = xmlDocument.CreateElement("", "配置数据", "");
			xmlDocument.AppendChild(newChild2);
			xmlDocument.Save(fileName);
		}

		public static void CreateTempXmlFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlNode newChild = xmlDocument.CreateXmlDeclaration("1.0", "gb2312", null);
			xmlDocument.AppendChild(newChild);
			XmlElement newChild2 = xmlDocument.CreateElement("", "零时数据", "");
			xmlDocument.AppendChild(newChild2);
			xmlDocument.Save(fileName);
		}

		public static void WriteTempData(string FileName, string NodeName, List<string> ConfigStrs, Dictionary<string, string> ConfigDict)
		{
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(FileName);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("零时数据");
				bool flag = false;
				for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
				{
					if (xmlNode.ChildNodes[i].Name == NodeName)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					XmlElement xmlElement = xmlDocument.CreateElement(NodeName);
					foreach (string name in ConfigStrs)
					{
						XmlText newChild = xmlDocument.CreateTextNode("");
						xmlNode.AppendChild(newChild);
						xmlElement.SetAttribute(name, "");
						xmlNode.AppendChild(xmlElement);
					}
					xmlDocument.AppendChild(xmlNode);
					xmlDocument.Save(FileName);
					xmlDocument.Load(FileName);
					xmlNode = xmlDocument.SelectSingleNode("零时数据");
				}
				foreach (object obj in xmlNode)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if (xmlNode2.Name == NodeName)
					{
						foreach (KeyValuePair<string, string> keyValuePair in ConfigDict)
						{
							try
							{
								xmlNode2.Attributes[keyValuePair.Key].Value = keyValuePair.Value;
							}
							catch
							{
							}
						}
					}
				}
				xmlDocument.Save(FileName);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		public static void WriteGlobleConfig(string fileName, Dictionary<string, string> configDict)
		{
			FileInfo fileInfo = new FileInfo(fileName);
			fileInfo.Attributes = FileAttributes.Normal;
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(fileName);
			XmlNode xmlNode = xmlDocument.SelectSingleNode("配置数据");
			List<string> globleData = ConfigData.GlobleData;
			bool flag = false;
			for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
			{
				if (xmlNode.ChildNodes[i].Name == "全局配置")
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				XmlElement xmlElement = xmlDocument.CreateElement("全局配置");
				foreach (string name in globleData)
				{
					XmlText newChild = xmlDocument.CreateTextNode("");
					xmlNode.AppendChild(newChild);
					xmlElement.SetAttribute(name, "");
					xmlNode.AppendChild(xmlElement);
				}
				xmlDocument.AppendChild(xmlNode);
				xmlDocument.Save(fileName);
				xmlDocument.Load(fileName);
				xmlNode = xmlDocument.SelectSingleNode("配置数据");
			}
			foreach (object obj in xmlNode)
			{
				XmlNode xmlNode2 = (XmlNode)obj;
				if (xmlNode2.Name == "全局配置")
				{
					foreach (KeyValuePair<string, string> keyValuePair in configDict)
					{
						try
						{
							xmlNode2.Attributes[keyValuePair.Key].Value = keyValuePair.Value;
						}
						catch
						{
						}
					}
				}
			}
			xmlDocument.Save(fileName);
			fileInfo.Attributes = FileAttributes.Hidden;
		}

		public static string GetDataFromConfigFile(string FileName, string noodName, string attributeName)
		{
			string result = "";
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(FileName);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("配置数据");
				foreach (object obj in xmlNode)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if (xmlNode2.Name == noodName)
					{
						result = xmlNode2.Attributes[attributeName].Value;
						break;
					}
				}
			}
			catch
			{
			}
			return result;
		}

		public static string GetDataFromTempFile(string FileName, string noodName, string attributeName)
		{
			string result = "";
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(FileName);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("零时数据");
				foreach (object obj in xmlNode)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					if (xmlNode2.Name == noodName)
					{
						result = xmlNode2.Attributes[attributeName].Value;
						break;
					}
				}
			}
			catch
			{
			}
			return result;
		}
	}
}
